package cn.quang.search.toolkit.provider.entity;

public class Result<T> {

    private static final int SUCCESS = 0;
    private static final int FAILURE = 1;

    private int code;
    private String message;
    private T value;

    public static <T> Result<T> success(T value) {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS);
        result.setMessage("成功");
        result.setValue(value);
        return result;
    }

    public static <T> Result<T> failure(String message) {
        Result<T> result = new Result<>();
        result.setCode(FAILURE);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> failure(int code, String message) {
        if(code == SUCCESS) {
            throw new IllegalStateException("CODE = " + SUCCESS + "不是失败状态码！");
        }
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public boolean isFail(){
        return this.code != SUCCESS;
    }

}
